package com.atguigu.gulimallseckill.scheduled;

import com.atguigu.gulimallseckill.service.SeckillService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class SeckillSkuScheduled {
    @Autowired
    SeckillService seckillService;
    @Autowired
    RedissonClient redissonClient;
    private final String UPLOAD_LOCK = "seckill:upload:lock";
    @Scheduled(cron = "0 * * * * ?")
//    @Scheduled(fixedRate = 10000)
    public void uploadSeckillSkuScheduled(){
        log.info("方法执行了");
        //获取一个锁
        RLock lock = redissonClient.getLock(UPLOAD_LOCK);
        //加锁并设置过期时间
        lock.lock(10, TimeUnit.SECONDS);
        try {
            seckillService.uploadSeckillSkuLatest3Days();
        }finally {
            //进行解锁
            lock.unlock();
        }
    }

}
